<template>
	<view class="userinfo">
		<view class="text">
			<image src="../static/Group1410137841@2x(2).png" mode=""></image>
			<view class="">{{$t('userinfo.name')}}</view>
		</view>
		<view class="flex-between">
			<view class="avatar-wrap" @click="chooseImage">
				<image class="avatar" :src="tempAvatar || userInfo.avatar || '/static/icon/default-avatar.png'" mode="aspectFill"></image>
				<!-- 进度遮盖 -->
				<view class="progress center" :class="{
						'no-transtion': uploadProgress === 0,
						show: uploadProgress != 100
					}"
				 :style="{
						width: uploadProgress + '%',
						height: uploadProgress + '%',
					}"></view>
			</view>
			<view class="rbtn" @click="confirm">{{$t('userinfo.name1')}}</view>
		</view>
		
		<view class="username">
			<view class="left">
				<view class="name">{{userInfo.nickname}}</view>
				<view class="userid">
					<view class="">{{$t('user.name26')}}:{{userInfo.username}}</view>
					<view class="copy" @click="copy(userInfo.username)">{{$t("copy")}}</view>
				</view>
			</view>
			<view class="rbtn" @click="$fun.jump(`/pageB/amend?name=${$t('userinfo.name2')}`)">{{$t('userinfo.name2')}}</view>
		</view>
		<view class="username">
			<view class="left">
				<view class="userid">
					<view class="">{{$t('user.name65')}}:{{userInfo.mobile}}</view>
					<view class="copy" @click="copy(userInfo.mobile)">{{$t("copy")}}</view>
				</view>
			</view>
		</view>
		<!-- <view class="username">
			<view class="left">
				<view class="userid">
					<view class="">{{$t('user.name89')}}:{{userInfo.address}}</view>
					<view class="copy" @click="copy(userInfo.address)">{{$t("copy")}}</view>
				</view>
			</view>
		</view> -->
	</view>
</template>

<script>
	import uniCopy from '@/components/xb-copy/uni-copy.js'
	export default {
		data() {
			return {
				uploadProgress: 100, //头像上传进度
				tempAvatar: '',
				userInfo: {},
				dateString: '',
				type: 'date',
			}
		},
		computed: {
			indicatorStyle() {
				return {
					background: 'rgba(255,0,0,.2)',
					height: '40px',
				};
			}
		},
		onLoad() {
			this.$fun.ajax.post('user/index', {}).then(res => {
				if (res.code == 1) {
					this.userInfo = res.data
				}
			})
			uni.setNavigationBarTitle({
				title:this.$t('user.name24')
			})
		},
		methods: {
			/**
			 * isShowToolBar：true 点击确定获取数值
			 * isShowToolBar：false 滑动获取返回数据
			 */
			dateTimeChange(value) {
				this.userInfo.birthday = value
			},
			copy(text) {
				uniCopy({
					content: text,
					success: (res) => {
						uni.showToast({
							title: res,
							icon: 'none'
						})
					},
					error: (e) => {
						uni.showToast({
							title: e,
							icon: 'none',
							duration: 3000,
						})
					}
				})
			},
			//提交修改
			async confirm() {
				const that =this
				const {
					userInfo,
				} = this;
				if (!userInfo.avatar && userInfo.avatar.slice(0, 10) != 'data:image') {
					this.$fun.msg(this.$t('user.name29'));
					// this.$refs.confirmBtn.stop();
					return;
				}
				if (!userInfo.nickname) {
					this.$fun.msg(this.$t('user.name27'));
					// this.$refs.confirmBtn.stop();
					return;
				}
				let userDate = {
					avatar: userInfo.avatar,
					nickname: userInfo.nickname,
				}
				const res = await this.$fun.ajax.post('/user/profile',{ ...userDate
				}).then(res => {
					if (res.code == 1) {
						this.$fun.msg(this.$t('user.name30'))
						setTimeout(()=>{
							that.$fun.jump('/pages/user/user',3)
						},1200)
					}
				})
			},
			//选择头像
			chooseImage() {
				let _this = this;
				uni.chooseImage({
					count: 1,
					success: res => {
						uni.showLoading({
							title: this.$t('user.name33'),
						});
						const tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							url: this.$baseUrl + '/api/common/upload', // 仅为示例，非真实的接口地址 
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								token: uni.getStorageSync('token'),
								contentType: "application/json"
							},
							success(res) {
								var res = JSON.parse(res.data)
								_this.userInfo.avatar = res.data.fullurl
								uni.showToast({
									title: res.msg,
									icon: "none"
								});
								setTimeout(() => {
									uni.hideLoading()
								}, 1000);
							},
						})
					}
				});
			},
			//裁剪回调
			async setAvatar(filePath) {
				this.tempAvatar = filePath;
				this.uploadProgress = 0;
				const result = await uniCloud.uploadFile({
					filePath: filePath,
					cloudPath: +new Date() + ('000000' + Math.floor(Math.random() * 999999)).slice(-6) + '.jpg',
					onUploadProgress: e => {
						this.uploadProgress = Math.round(
							(e.loaded * 100) / e.total
						);
					}
				});
				if (!result.fileID) {
					this.$util.msg(this.$t('user.name32'));
					return;
				}
				if (typeof uniCloud.getTempFileURL === 'undefined') {
					this.userInfo.avatar = result.fileID;
				} else {
					const tempFiles = await uniCloud.getTempFileURL({
						fileList: [result.fileID]
					})
					const tempFile = tempFiles.fileList[0];
					if (tempFile.code === 'SUCCESS') {
						this.userInfo.avatar = tempFile.download_url;
					} else {
						this.$util.msg(this.$t('user.name32'));
					}
				}
			},
			//修改性别
			changeGender(gender) {
				this.userInfo.gender = gender
			},
			//公开信息
			onSwitch(e) {
				this.userInfo.anonymous = !e.detail.value;
			}
		}
	}
</script>

<style lang="scss">
	.userinfo{
		padding: 20rpx 30rpx;
		background-color: #00A3FF12;
		width: 690rpx;
		// height: 258rpx;
		margin: 50rpx auto;
		border-radius: 20rpx;
		.text{
			display: flex;
			align-items: center;
			// margin: 30rpx 0;
			image{
				width: 24rpx;
				height: 18rpx;
				margin-right: 8rpx;
			}
			view{
				font-size: 30rpx;
				font-weight: 600;
				color: #333333;
			}
		}
		.avatar-wrap{
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
			overflow: hidden;
			position: relative;
			margin-top: 8rpx;
			.avatar{
				width: 100%;
				height: 100%;
			}
			.progress{
				position: absolute;
				top: 0;
				left: 0;
				border-radius: 50%;
				// background-color: rgba(0,0,0,.5);
				transition: all .3s;
				&.no-transtion{
					transition: none;
				}
				&.show{
					width: 100%;
					height: 100%;
				}
			}
		}
		.username{
			display: flex;
			justify-content: space-between;
			margin-top: 12rpx;
			.left{
				display: flex;
				width: 72%;
				flex-wrap: wrap;
				.name{
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 32rpx;
					color: #232323;
				}
				.userid{
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #232323;
					display: flex;
					align-items: center;
					margin-left: 8rpx;
					.copy{
						width: 60rpx;
						height: 24rpx;
						background: #01A3FF21;
						border-radius: 12rpx;
						font-weight: 300;
						font-size: 16rpx;
						color: #01A3FF;
						line-height: 24rpx;
						text-align: center;
						margin-left: 20rpx;
					}
				}
			}
			
		}
		.rbtn{
			// width: 120rpx;
			padding: 0 20rpx;
			height: 38rpx;
			background: linear-gradient( 180deg, #1DF3FF 0%, #0EA3FF 100%);
			border-radius: 19rpx 19rpx 19rpx 19rpx;
			line-height: 38rpx;
			text-align: center;
			font-size: 20rpx;
			font-weight: 500;
			color: #FFFFFF;;
		}
	}
</style>